package cm.studapp;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import cm.studapp.helpers.ButtonHelper;
import cm.studapp.helpers.StatisticHelper;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

public class StatisticsMain extends Activity implements OnItemSelectedListener {
	
	private DefaultRenderer mRenderer = new DefaultRenderer();
	private GraphicalView mChartView;
    private CategorySeries mSeries = new CategorySeries("");
    private static int[] COLORS = new int[] { Color.GREEN, Color.RED };
    private Spinner typeSpinner;
    private Spinner timeSpinner;
	private StatisticHelper statHelper;
	private ButtonHelper btHelper;
	private String typeFilter, timeFilter;
    public StatisticsMain() {
		// TODO Auto-generated constructor stub
	}
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.statistics_main);
        typeSpinner = (Spinner) findViewById(R.id.StatMainSpn_type);
        typeSpinner.setOnItemSelectedListener(this);
        timeSpinner = (Spinner) findViewById(R.id.StatMainSpn_statDuration);
        timeSpinner.setOnItemSelectedListener(this);
        statHelper = new StatisticHelper(this);
        btHelper = new ButtonHelper(this);
        Button chartChange = (Button) findViewById(R.id.StatMainBtn_chartChange);
        chartChange.setOnClickListener(btHelper);

    }

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		// TODO Auto-generated method stub

		String asdf = (String)parent.getItemAtPosition(pos);
		asdf1();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	//Metodo bonus para demostrar cambios dinamicos en el grafo: 
	public void asdf1(){
		typeFilter = (String)typeSpinner.getSelectedItem();
		timeFilter = (String)timeSpinner.getSelectedItem();		
		int [] input = new int[2];
		
        //codigo bonus de prueba:
		mRenderer = new DefaultRenderer();
        mRenderer.setApplyBackgroundColor(true);
        //mRenderer.setBackgroundColor(R.drawable.backgroundgradient);
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setShowLabels(false);
        mRenderer.setLegendTextSize(20);
        mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setStartAngle(90);
        mRenderer.setPanEnabled(true);
        mSeries.clear();
        input = statHelper.getStats(typeFilter,timeFilter);
        /*
        //Agregar datos al chart:
        if(type.equals("Todos")){
        	input = statHelper.getTotalStats();
        }
        else if(type.equals("Tareas")){
        	input = statHelper.getPartialStats("tarea");
        }
        else{
        	input = statHelper.getPartialStats("estudio");
        	
        }
        */
    	mSeries.add("Cumplidos", input[0]);
    	SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
        renderer.setColor(COLORS[0]);
        mRenderer.addSeriesRenderer(renderer);


        mSeries.add("No cumplidos", input[1]);
    	renderer = new SimpleSeriesRenderer();
        renderer.setColor(COLORS[1]);
        mRenderer.addSeriesRenderer(renderer);
        
  /*      
        if(type.equals("Todos")){
        	mSeries.add("Cumplidos", input[0]);
        	SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
            renderer.setColor(COLORS[0]);
            mRenderer.addSeriesRenderer(renderer);


            mSeries.add("No cumplidos", input[1]);
        	renderer = new SimpleSeriesRenderer();
            renderer.setColor(COLORS[1]);
            mRenderer.addSeriesRenderer(renderer);
        }   
        else if(type.equals("Tareas")){
        	int[] input = statHelper.getPartialStats("tareas");
        	mSeries.add("Cumplidos", 10);
        	SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
            renderer.setColor(COLORS[0]);
            mRenderer.addSeriesRenderer(renderer);


            mSeries.add("No cumplidos", 60);
        	renderer = new SimpleSeriesRenderer();
            renderer.setColor(COLORS[1]);
            mRenderer.addSeriesRenderer(renderer);       	
        }
        */
            
            LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
            mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
            mRenderer.setClickEnabled(true);
            mRenderer.setSelectableBuffer(10);
            layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
   		
	}

}
